package com.develop.web.in;


import com.develop.data.loan.model.LoanContract;
import com.develop.web.in.service.LoanContractHubService;
import com.develop.web.in.service.api.LoanContractDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class LoanContractController {

    @Autowired
    LoanContractHubService loanContractHubService;
    @Autowired
    LoanContractDetailService<LoanContract, LoanContract> loanContractDetailService;

    //需要配置json序列化、反序列化，从json类型的body转换成controller入参数
    @PostMapping(path = "/v1/contract/create", consumes = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseStatus(HttpStatus.CREATED)
    List<LoanContract> confirm(@RequestBody List<LoanContract> items) {

        //items.forEach(item -> {
        //
        //    String productionNumber = item.getLoanProductionNumber();
        //    String businessNumber = item.getLoanBusinessNumber();
        //    LoanState loanState = item.getLoanState();
        //
        //    String serviceName = MessageFormat.format("com.develop.web.in.service.{0}{1}LoanInstallmentService", productionNumber, businessNumber);
        //    LoanRepaymentPlanPreviewService<LoanContract, LoanInstallment> service;
        //    try {
        //        service = (LoanRepaymentPlanPreviewService<LoanContract, LoanInstallment>) AppContext.getBean(Class.forName(serviceName));
        //    } catch (ClassNotFoundException e) {
        //        throw new RuntimeException(e);
        //    }
        //
        //    List<LoanInstallment> loanInstallments = service.preview(item);
        //    item.setLoanInstallments(loanInstallments);
        //});
        //return loanContractCreateService.create(items);

        return loanContractHubService.batchCreate(items);


    }

    @PostMapping(path = "/v1/contract/detail", consumes = {MediaType.APPLICATION_JSON_VALUE} ,produces = "application/json")
    @ResponseStatus(HttpStatus.OK)
    List<LoanContract> detail(@RequestBody List<LoanContract> items) {
        return loanContractDetailService.batchDetail(items);
    }

    //需要配置表单，从表单转化成controller入参
    //form-data与x-www-form-urlencoded的区别
    @PostMapping(path = "/v2/contract", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}, produces = "application/json")
    LoanContract test(LoanContract loanContract) {
        return loanContract;
    }
}
